const global = getApp().globalData;
const userManager = require('../../../js/userManager');
Page({
  data: {
    refuseAuth: false,
    authShow: false,
    loadingImg: ''
  },
  onShow() {
    console.log('on auth page show....');
  },
  onLoad() {
    // this.onLoading();
    let userAuthFlag = userManager.getRefuseAuth();
    this.setData({
      refuseAuth: userAuthFlag
    })
    wx.setNavigationBarTitle({
      title: '朵威教育授权登录'
    });
  },
  onGetUserInfo(res) {
    console.log(res);
    let self = this;
    let userInfo = res.detail.userInfo;
    // 存储用户决绝授权状态
    userManager.setRefuseAuth(!userInfo);
    if ( !userInfo ) {
      self.setData({
        refuseAuth: true
      })
      return;
    }
    this.authLogin();
  },
  authLogin() {
    userManager.login({
      auth: true,
      success() {
        console.log('授权登录成功');
        global.store.initialData()
        wx.switchTab({
          url: '/pages/index/index'
        })
      },
      noAuth() {
        
      },
      noBind() {
        console.log('前往绑定');
        wx.redirectTo({
          url: '../bind/bind'
        })
      }
    })
  },
  numCirculate( num, time, callback ) {

    let index = 0;
    
    let interval = setInterval(() => {
      index ++;
      if ( index > num ) {
        index = 1
      }
      if ( callback( index ) ) {
        clearInterval( interval );
      }
    }, time * 1000);
  },
  authSetting() {

  }
}) 
